package com.starbaba.stat.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.stat.model.StatShortLinkPVUV;
import com.starbaba.stat.service.StatShortLinkPVUVService;
import com.starbaba.stat.utils.Constant;

@SuppressWarnings("rawtypes")
@Component
public class StatShortLinkPVUVServiceImpl implements StatShortLinkPVUVService {

	static BaseDao dao = new BaseDao(Constant.LOAN_STAT_DB_POOL);

	@Override
	public void showPage(Map<String, Object> paramsMap) {
	}

	@Override
	public List getList(int start, int length, PageResponse pageResponse, Map<String, Object> paramsMap)
			throws Exception {

		List<StatShortLinkPVUV> list = null;
		String short_link = (String) paramsMap.get("short_link");
		String time = (String) paramsMap.get("time");
		String startDate = (String) paramsMap.get("startDate");
		String endDate = (String) paramsMap.get("endDate");
		String activity = (String) paramsMap.get("activity");
		String memo = (String) paramsMap.get("memo");

		StringBuffer list_sql = new StringBuffer(
				"SELECT cdate time, short_link, pv, uv, type, activity, unit_privce, total_price_pv, total_price_uv,memo FROM stat_loan_short_link WHERE 1=1 ");
		StringBuffer total_sql = new StringBuffer("SELECT count(1) FROM stat_loan_short_link WHERE 1=1 ");

		if (short_link != null) {
			list_sql.append(" AND ( ");
			total_sql.append(" AND ( ");
			if (short_link.contains(",")) {
				String[] short_linkArray = short_link.split(",");
				for (int i = 0; i < short_linkArray.length; i++) {
					String short_link_r = short_linkArray[i];
					if (i != short_linkArray.length - 1) {
						list_sql.append(" short_link = '" + short_link_r + "' or ");
						total_sql.append(" short_link = '" + short_link_r + "' or ");
					} else {
						list_sql.append(" short_link = '" + short_link_r + "' ");
						total_sql.append(" short_link = '" + short_link_r + "' ");
					}
				}
			} else {
				list_sql.append(" short_link = '" + short_link + "' ");
				total_sql.append(" short_link = '" + short_link + "' ");
			}
			list_sql.append(")");
			total_sql.append(")");
		}

		if (memo != null) {
			list_sql.append(" AND ( ");
			total_sql.append(" AND ( ");
			if (memo.contains(",")) {
				String[] memoArray = memo.split(",");
				for (int i = 0; i < memoArray.length; i++) {
					String memo_r = memoArray[i];
					if (i != memoArray.length - 1) {
						list_sql.append(" memo LIKE '%" + memo_r + "%' or ");
						total_sql.append(" memo LIKE '%" + memo_r + "%' or ");
					} else {
						list_sql.append(" memo LIKE '%" + memo_r + "%' ");
						total_sql.append(" memo LIKE '%" + memo_r + "%' ");
					}
				}
			} else {
				list_sql.append(" memo LIKE '%" + memo + "%' ");
				total_sql.append(" memo LIKE '%" + memo + "%' ");
			}
			list_sql.append(")");
			total_sql.append(")");
		}

		if (time != null) {
			list_sql.append("AND cdate = '" + time + "' ");
			total_sql.append("AND cdate = '" + time + "' ");
		}

		if (startDate != null) {
			list_sql.append("AND cdate >= '" + startDate + "' ");
			total_sql.append("AND cdate >= '" + startDate + "' ");
		}

		if (endDate != null) {
			list_sql.append("AND cdate <= '" + endDate + "' ");
			total_sql.append("AND cdate <= '" + endDate + "' ");
		}

		if (activity != null) {
			list_sql.append("AND activity LIKE '%" + activity + "%' ");
			total_sql.append("AND activity LIKE '%" + activity + "%' ");
		}
		list_sql.append("ORDER BY cdate DESC,pv DESC LIMIT " + start + "," + length);

		int count = 0;
		try {
			list = dao.list(list_sql.toString(), StatShortLinkPVUV.class);
			count = dao.getInt(total_sql.toString());
			for (StatShortLinkPVUV model : list) {
				model.setShort_link(com.starbaba.model.dictionary.Constant.COMMON_HOST
						+ "/share_service/redirect?from=" + model.getShort_link());
			}
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);

		} catch (AppException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public String getExportString(List<StatShortLinkPVUV> list) {
		StringBuilder builder = new StringBuilder();
		builder.append("日期,短链接,pv,uv,类型,备注,activity,单价\r\n");
		for (StatShortLinkPVUV vo : list) {
			builder.append(vo.getTime()).append(",");
			builder.append(vo.getShort_link()).append(",");
			builder.append(vo.getPv()).append(",");
			builder.append(vo.getUv()).append(",");
			builder.append(vo.getType()).append(",");
			builder.append(vo.getMemo()).append(",");
			builder.append(vo.getActivity()).append(",");
			builder.append(vo.getUnit_privce()).append(",");
			builder.append("\r\n");
		}
		return builder.toString();
	}
}
